#星期五下午九年课程冲突检查
import config
class c:
    def __init__(self,db,rp,log):
        self.db=db
        self.rp=rp 
        self.cur=self.db.cursor()
        self.log=log
        self.teacherList=[]
        self.g9=config.grade['九年级']+config.AdditionalClass
        self.g78=config.grade['七年级']+config.grade['八年级']
        for i in config.AdditionalClass:
            self.g78.remove(i)
        self.main()
    def main(self):
        self.rp.i("\n\n开始检查星期五下午九年级课程冲突")
        sql="select name from main group by name"
        r=self.cur.execute(sql)
        for i in r.fetchall():
            self.teacherList.append(i[0])
        for i in self.teacherList:
            r=self.cur.execute("select name,class,real_course,times from main where name='{}' and week=5 and times>{}  ORDER BY times ASC;".format(i,config.MorningSession[-1]))
            r=r.fetchall()
            self.check_course(r)
        self.check_ITcourse()
        self.rp.i("完成星期五下午九年级课程冲突检查")
    def check_ITcourse(self): #信息课冲突检查
        r=self.cur.execute("select name,class,real_course,times from main where real_course='信息' and week=5 and times>{}  ORDER BY times ASC;".format(config.MorningSession[-1]))
        r=r.fetchall()
        self.check_course(r,"(信息课)")
    def check_course(self,list,course=""):
        for i in list[:-1]:
            s=None
            if(i[1] in self.g9):
                s=list[list.index(i)+1]
                if((s[1] in self.g78 ) and int(i[3])==int(s[3])-1):
                    self.rp.e("{}星期五{}第{}节{}和{}第{}节{}补课延迟后冲突{}".format(i[0],i[1],str(int(i[3])+1),i[2],s[1],str(int(s[3])+1),s[2],course))
                



if __name__ == "__main__":
    import sqlite3
    import loger
    import config
    import pathlib
    import reportTotext
    log = loger.Logger('timetable',StreamLevel='debug',FileLevel='info',fmt='%(asctime)s-%(levelname)s:\t%(message)s').logger
    root=pathlib.Path(__file__).parent
    db=sqlite3.connect(str(root/"classbook.db"))
    cur = db.cursor()
    r=cur.execute("select v from setting where k = 'filepath';")
    r=r.fetchone()
    filepath=r[0]
    rp=reportTotext.reportTotext(filepath,log)

    m=c(db,rp,log)
    rp.save()